{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "bebab06a-930c-448b-8c87-d61c019ee36d",
   "metadata": {},
   "source": [
    "# Ollama Llama Pack Example"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "3863b938-c410-42ea-be12-30cff5e5c568",
   "metadata": {},
   "source": [
    "### Setup Data"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "c5cd7c94-05ba-4385-958c-4ecb50907295",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "--2023-12-06 10:57:29--  https://www.dropbox.com/s/f6bmb19xdg0xedm/paul_graham_essay.txt?dl=1\n",
      "Resolving www.dropbox.com (www.dropbox.com)... 2620:100:6057:18::a27d:d12, 162.125.13.18\n",
      "Connecting to www.dropbox.com (www.dropbox.com)|2620:100:6057:18::a27d:d12|:443... connected.\n",
      "HTTP request sent, awaiting response... 302 Found\n",
      "Location: /s/dl/f6bmb19xdg0xedm/paul_graham_essay.txt [following]\n",
      "--2023-12-06 10:57:29--  https://www.dropbox.com/s/dl/f6bmb19xdg0xedm/paul_graham_essay.txt\n",
      "Reusing existing connection to [www.dropbox.com]:443.\n",
      "HTTP request sent, awaiting response... 302 Found\n",
      "Location: https://uc2fc064df073edb14568cb68878.dl.dropboxusercontent.com/cd/0/get/CI6sL69BcU1vwbM-TAz-tGmRw4WlbgfQuJZeNXywf2hq34Y0hCCL--A7gVBWo6T3igijCNDkLGnPwDiugV9pIEtnFODeIcET5PwUgToHl2-1P77MqJy2okrGt6CmC5bxwI5OPEV4MGsUNvSHP34FLMWZ/file?dl=1# [following]\n",
      "--2023-12-06 10:57:30--  https://uc2fc064df073edb14568cb68878.dl.dropboxusercontent.com/cd/0/get/CI6sL69BcU1vwbM-TAz-tGmRw4WlbgfQuJZeNXywf2hq34Y0hCCL--A7gVBWo6T3igijCNDkLGnPwDiugV9pIEtnFODeIcET5PwUgToHl2-1P77MqJy2okrGt6CmC5bxwI5OPEV4MGsUNvSHP34FLMWZ/file?dl=1\n",
      "Resolving uc2fc064df073edb14568cb68878.dl.dropboxusercontent.com (uc2fc064df073edb14568cb68878.dl.dropboxusercontent.com)... 2620:100:6057:15::a27d:d0f, 162.125.13.15\n",
      "Connecting to uc2fc064df073edb14568cb68878.dl.dropboxusercontent.com (uc2fc064df073edb14568cb68878.dl.dropboxusercontent.com)|2620:100:6057:15::a27d:d0f|:443... connected.\n",
      "HTTP request sent, awaiting response... 200 OK\n",
      "Length: 75047 (73K) [application/binary]\n",
      "Saving to: ‘paul_graham_essay.txt’\n",
      "\n",
      "paul_graham_essay.t 100%[===================>]  73.29K  --.-KB/s    in 0.02s   \n",
      "\n",
      "2023-12-06 10:57:30 (4.02 MB/s) - ‘paul_graham_essay.txt’ saved [75047/75047]\n",
      "\n"
     ]
    }
   ],
   "source": [
    "!wget \"https://www.dropbox.com/s/f6bmb19xdg0xedm/paul_graham_essay.txt?dl=1\" -O paul_graham_essay.txt"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "0408cf88-a4ca-4700-9f01-21af1f1994c5",
   "metadata": {},
   "outputs": [],
   "source": [
    "from llama_index.core import SimpleDirectoryReader\n",
    "\n",
    "# load in some sample data\n",
    "reader = SimpleDirectoryReader(input_files=[\"paul_graham_essay.txt\"])\n",
    "documents = reader.load_data()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "31073b9b-1fea-438c-98af-61ff5e256dd1",
   "metadata": {},
   "source": [
    "### Start Ollama\n",
    "\n",
    "Make sure you run `ollama run llama2` in a terminal."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "f0cc8558-90b1-4224-a075-c6fd6da92262",
   "metadata": {},
   "outputs": [],
   "source": [
    "# !ollama run llama2"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "60c0dc51-03ed-4a6e-b1e1-e8e45fbd203d",
   "metadata": {},
   "source": [
    "### Download and Initialize Pack\n",
    "\n",
    "We use `download_llama_pack` to download the pack class, and then we initialize it with documents.\n",
    "\n",
    "Every pack will have different initialization parameters. You can find more about the initialization parameters for each pack through its [README](https://github.com/logan-markewich/llama-hub/tree/main/llama_hub/llama_packs/voyage_query_engine) (also on LlamaHub).\n",
    "\n",
    "**NOTE**: You must also specify an output directory. In this case the pack is downloaded to `voyage_pack`. This allows you to customize and make changes to the file, and import it later! "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "4aa48521-e0aa-410a-9072-212862c534b8",
   "metadata": {},
   "outputs": [],
   "source": [
    "from llama_index.core.llama_pack import download_llama_pack\n",
    "\n",
    "# download and install dependencies\n",
    "OllamaQueryEnginePack = download_llama_pack(\n",
    "    \"OllamaQueryEnginePack\", \"./ollama_pack\"\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "f8b16200-8a8e-48d8-8546-3c843d452a0e",
   "metadata": {},
   "outputs": [],
   "source": [
    "# You can use any llama-hub loader to get documents!\n",
    "ollama_pack = OllamaQueryEnginePack(model=\"llama2\", documents=documents)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "7b0d86f9-e8db-44b1-b183-3537706e101b",
   "metadata": {},
   "outputs": [],
   "source": [
    "response = ollama_pack.run(\"What did the author do growing up?\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "37945369-d55d-4c24-a0be-43548de7dbf1",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Based on the information provided in the context, the author did not mention anything about what he did growing up. The text only covers his experiences as an adult, including his work at Viaweb, Y Combinator, and his interest in painting. There is no information given about the author's childhood or formative years.\n"
     ]
    }
   ],
   "source": [
    "print(str(response))"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "llama_index_v2",
   "language": "python",
   "name": "llama_index_v2"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
